package vincent.com.layouttest.anim;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import vincent.com.layouttest.ColorStatusActivity;
import vincent.com.layouttest.R;

/**
 * Created by zhengwenchen on 2016/4/15.
 */
public class FadeAnimSampleActivity extends ColorStatusActivity {

    private ImageView mAlbumImg;

    private ObjectAnimator mFadeOutAnim;

    private ObjectAnimator mFadeInAnim;

    private int mImgIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_fade_anim_sample);

        mAlbumImg = (ImageView)findViewById(R.id.album_img);

        mFadeOutAnim = ObjectAnimator.ofFloat(mAlbumImg, "alpha", 1f, 0f);
        mFadeInAnim = ObjectAnimator.ofFloat(mAlbumImg, "alpha", 0f, 1f);

        mFadeInAnim.setDuration(500);
        mFadeOutAnim.setDuration(500);

        mFadeOutAnim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(mImgIndex == 0){
                    mImgIndex = 1;
                    mAlbumImg.setImageResource(R.drawable.my_myalbum_img_2);
                }else if(mImgIndex == 1){
                    mImgIndex = 0;
                    mAlbumImg.setImageResource(R.drawable.my_myalbum_img);
                }
                mFadeInAnim.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        mFadeInAnim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.activity_fade_anim_sample, rootView, true);
    }

    public void startAnim(View v){
        mFadeOutAnim.start();
    }

    @Override
    public String getToolbarTitle() {
        return "Fade Animation";
    }
}
